<?php

namespace app\api\controller;
use app\api\controller\Tpmecms;
use app\api\library\Bt as Baota;
use think\Db;




/**
 * 我的
 */
class Bt extends Tpmecms
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    private $BT_KEY = "wLPAb0CZIZiNCn3VhIfspNE8PYshbpPv";  //接口密钥
    private $BT_PANEL = "http://bt2.he4966.cn:4966";	   //面板地址


    public function _initialize()
    {
        parent::_initialize();
        
    }
  
    public function update()
    {
        // $bt = new Baota()
        $list = Db::name('bt')->select();
        $error = 0;
        $success =0;
        foreach ($list as $key => $val) {
            $bt = new Baota($val['ip'], $val['key']);
            // header('Content-type: application/json');
            $data = ($bt->GetSystemTotal());

            // halt($data);
            if(isset($data['status']) || $data==null){
                if(!$data['status'] && $val['status']=='2'){
                    $up_data['error'] = $data['msg'];
                    $up_data['status'] = '1';//1失效 2正常
                    Db::name('bt')->where('id',$val['id'])->update($up_data);
                    $error++;
                }

            }elseif($val['status']=='1'){
                unset($up_data);
                $up_data['error'] = '';
                $up_data['status'] = '2';//1失效 2正常
                Db::name('bt')->where('id',$val['id'])->update($up_data);
                $success++;
            }

        }
        $this->success('ok',['error'=>$error,'success'=>$success]);
    }

    public function get($name = 'GetNetWork')
    {
        $name = 'GetNetWork';//如果需要自行传参数请注释 注意权限问题
        $list = Db::name('bt')->where('status',2)->select();
        $data = [];
        foreach ($list as $key => $val) {
            $bt = new Baota($val['ip'], $val['key']);
            // header('Content-type: application/json');
            $temp = $bt->$name();
            $temp['name'] = $val['name'];
            $data[$val['id']] = $temp;
        }
        $this->success('ok',$data);
        // halt($data);
    }
}